package com.rois.librois.libRoISMessageStorage;

import java.util.Vector;

/**
 * Data holder class for exchanged message.
 * This object holds a list of:
 * - message types
 * - message IDs
 * - message parameters
 * - status
 *
 * @author Miki Sato <miki.sato.1225@gmail.com>
 *
 *    The MIT licence
 *
 *    Copyright (C) 2011
 *      ATR Intelligent Robotics and Communication Laboratories, Japan
 *
 *    Permission is hereby granted, free of charge, to any person obtaining a copy
 *    of this software and associated documentation files (the "Software"), to deal
 *    in the Software without restriction, including without limitation the rights
 *    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 *    of the Software, and to permit persons to whom the Software is furnished to do so,
 *    subject to the following conditions:
 *
 *    The above copyright notice and this permission notice shall be included in all
 *    copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 *    INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *    PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 *    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
public class RoIS_ExchangedMessages {
	private static final int DEFAULT_MSG_MAXSIZE = 100;
	public int size;
	public int max_size;
	public Vector<String> type = new Vector<String>();
	public Vector<String> id = new Vector<String>();
	public Vector<RoIS_Parameters> parameters = new Vector<RoIS_Parameters>();
	public Vector<String> status = new Vector<String>();

	/**
	 * create data holder
	 */
	public RoIS_ExchangedMessages()
	{
		max_size = DEFAULT_MSG_MAXSIZE;
		clear();
	}

	/**
	 * clear all data.
	 */
	public void clear()
	{
		size = 0;
		type.clear();
		id.clear();
		parameters.clear();
		status.clear();
	}

	/**
	 * add message at he end of list.
	 * @param _messagetype message type
	 * @param _messageid message ID
	 */
	public void push_back(
		String _messagetype,
		String _messageid
	)
	{
		type.add(_messagetype);
		id.add(_messageid);
		RoIS_Parameters _parameters = new RoIS_Parameters();
		parameters.add(_parameters);
		status.add("");
		size++;

		if(size>max_size)
		{
			erase(0);
		}
	}

	/**
	 * remove message at the end of list.
	 */
	public void pop_back()
	{
		if(size>0)
		{
			type.remove(type.size()-1);
			id.remove(id.size()-1);
			parameters.remove(parameters.size()-1);
			status.remove(status.size()-1);
			size--;
		}
	}

	/**
	 * remove specified message from list.
	 * @param index index of the message
	 */
	public void erase(
		int index
	)
	{
		if(size>0)
		{
			type.remove(index);
			id.remove(index);
			parameters.remove(index);
			status.remove(index);
			size--;
		}
	}

	/**
	 * get message index related to message ID.
	 * @param _id Id of the message
	 * @return index of the message
	 */
	public int getIndexById(
		String _id
	)
	{
		int iRes = -1;
		for(int i=0; i<size; i++)
		{
			if(id.get(i).matches(_id))
			{
				iRes = i;
				break;
			}
		}
		return iRes;
	}
}
